//
//  EmoticonManager.swift
//  Emoticon
//
//  Created by 李锡鹏 on 2017/6/10.
//  Copyright © 2017年 李锡鹏. All rights reserved.
//

import Foundation
class EmoticonManager {
    // 单例
    static let sharedManager = EmoticonManager()
    lazy var packages = [EmoticonPackage]()
    private init() {
        //1 读取Emoticon.plist的路径
        guard let path = Bundle.main.path(forResource: "emoticons", ofType: "plist", inDirectory: "Emoticons.bundle") else {
            print("找不到emoticons.plist 文件")
            return
        }
        //2 用路径读取文件 加载为dictionary的类型
        guard let dict = NSDictionary(contentsOfFile: path) else {
            print("加载数据出错")
            return
        }
        //3 读取packages下面的id值 存到数组里
        let array = (dict["packages"] as! NSArray).value(forKey: "id") as! [String]
        //4 用id加载表情包
        packages.append(EmoticonPackage(dict: ["group_name_cn":"最近A" as AnyObject]))
        for item in array {
            // 取路径
            let infoListPath = Bundle.main.path(forResource: "info", ofType: "plist", inDirectory: "Emoticons.bundle/\(item)")
            // 用路径读数据
            let infoDict = NSDictionary(contentsOfFile: infoListPath!)
            // 加载数据
            packages.append(EmoticonPackage(dict: infoDict as! [String : AnyObject]))
        }
    }
}
